/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package music;

import java.awt.Graphics;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.Vector;
import javax.sound.midi.Sequence;
import music.types.NoteTypes;

/**
 *
 * @author TJ
 */
public class Score {
    private HashMap<Integer, TimeSignature> timeChanges;
    private HashMap<Integer, Tempo> tempoChanges;
    private HashMap<Integer, KeySignature> keyChanges;
    public static final int MAX_DIVISION=32;
     private SpacingOptions spacing;
    private Vector<Staff> staves;
    private int measures=8;
    private boolean selectionMode=true;
    public Score(int m){
        init();
        def();
        measures=m;
    }
    public Score(TimeSignature t, Tempo t2, KeySignature k, int m){
        init();
        def();
        timeChanges.put(1,t);
        tempoChanges.put(1, new Tempo());
        keyChanges.put(1, k);
        measures=m;
    }
    private void def() {
        timeChanges.put(1,TimeSignature.COMMON_TIME);
        tempoChanges.put(1, new Tempo());
        keyChanges.put(1, new KeySignature("C",0));
    }
    private void init(){
        spacing=new SpacingOptions();
        timeChanges=new HashMap<Integer,TimeSignature>();
        tempoChanges=new HashMap<Integer,Tempo>();
        keyChanges=new HashMap<Integer,KeySignature>();
        staves=new Vector<Staff>();
        
        for(int i=0; i<measures; i++) {
            for(Staff s :staves) {
                s.addMeasure();
            }
        }
    }
    public SpacingOptions getSpacing(){return spacing;}
    public Staff getStaff(int index){return staves.get(index);}
    public Vector<Staff> getStaves(){return staves;}
    public void addStaff(Instrument i) {
        
    }
    public void addStaff(Staff f){
        f.setScore(this);
            while(f.countMeasures()>measures) {
                addMeasure();
            }
            while(f.countMeasures()<measures){
                f.addMeasure();
            }
        staves.add(f);
        
    }
    public void removeStaff(Staff f) {}
    public void removeStaff(int i){} 
    
    public void addMeasure() {
        for(Staff s : staves){
                s.addMeasure();
        }
        measures++;
    }
    public void addMeasure(Staff f, Measure m) {
        f.addMeasure(m);
        for(Staff s : staves){
            if(f!=s){
                s.addMeasure();
            }
        }
        measures++;
    }
    public void removeMeasure(){}
    public void changeTempo(int m, Tempo t) {}
    public void changeKey(int m, KeySignature t){}
    public void changeTime(int m,TimeSignature t){}
    
    public Tempo getTempoAt(int measure){
        if(tempoChanges.containsKey(measure)){return tempoChanges.get(measure);}
        Integer last=1;
        for(Integer i : tempoChanges.keySet()) {
            
            if(measure>=last && measure<i){
                return tempoChanges.get(i);
            }
            last=i;
        }
        return tempoChanges.get(last);
    }
    public KeySignature getKeyAt(int measure){
        if(keyChanges.containsKey(measure)){return keyChanges.get(measure);}
        Integer last=1;
        for(Integer i : keyChanges.keySet()) {
            
            if(measure>=last && measure<i){
                return keyChanges.get(i);
            }
            last=i;
        }
        return keyChanges.get(last);
    }
    public TimeSignature getTimeAt(int measure) {
        if(timeChanges.containsKey(measure)){return timeChanges.get(measure);}
        Integer last=1;
        for(Integer i : timeChanges.keySet()) {
            
            if(measure>=last && measure<i){
                return timeChanges.get(i);
            }
            last=i;
        }
        return timeChanges.get(last);
    }
    public void save(){}
    public void load(){}
    public Sequence createSequence(){return null;}
    public void play(){}
    public void pause(){}
    public void stop(){}
    public void render(Graphics g) {
        for(Staff s : staves) {
            s.render(g, 10);
        }
    }
}
